﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Zhongchouwwwww.Models;
using static System.DateTime;

namespace Zhongchouwwwww.webform
{
    public partial class project_add_item1 : System.Web.UI.Page
    {
        private static int ThisUserID;
        private static string filename = "";
        private static string URL = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!BAL.DataVerification.IsSignin())
                Response.Redirect("Index1.aspx");
            ThisUserID = Config.Config.ThisUser.UserId;
        }

        protected void nextStep_Click(object sender, EventArgs e)
        {
            if (projectname.Text == "" || targetmoney.Text == "" || timeDay.Text == "" || txtShow.Text == null || txtTb1.Text == "" || txtTb2.Text ==""|| txtTb3.Text ==""|| txtTb4.Text == ""|| txtTb5.Text =="" || ctiy2.SelectedValue == "")
            {
                Response.Write("<script>alert('带*的为必填项')</script>");
                return;
            }
            //验证图片上传
            var (是否合法, 违法原因, 文件名) = CheckFiles(FUloadimg);
            filename = 文件名;
            //图片合法则添加数据和上传图片
            if (!是否合法)
            {
                Response.Write($"<script>alert('{违法原因}')</script>");
            }
            else
            {
                try
                {
                    //文件名中加入随机数
                    int num = new Random().Next(0, 999);
                    //上传图片文件
                    FUloadimg.SaveAs(Server.MapPath("..") + "/images/" + (num) + filename);
                    //格式化字段  
                    URL = "../images/" + (num) + filename;
                    string sql = $"insert Projectinfo values('{projectname.Text}',{int.Parse(targetmoney.Text)},'{int.Parse(timeDay.Text)}','{type.SelectedValue}','{ctiy1.SelectedValue + ctiy2.SelectedValue}','{URL}','{txtTv.Text}','{txtShow.Text}','{txtTb1.Text}','{txtTb2.Text}','{txtTb3.Text}','{txtTb4.Text}','{txtTb5.Text}','{txtTb6.Text}','{txtTitle.Text}','0','草稿','0','{DateTime.Now}','{ThisUserID}',null,'1')";
                    //增加数据至数据库
                    if (BAL.GetDate.EXEXE(sql))
                    {
                        Config.Config.projectname = projectname.Text;
                        Response.Redirect("project-returns-set.aspx?id=1");
                    }
                    else
                    {
                        Response.Write("<script>alert('添加失败！')</script>");
                    }
                }
                catch (Exception)
                {
                    Response.Write("<script>alert('参数有误，请重新填写！')</script>");
                }
                
            }
        }

        /// <summary>
        /// 验证上传封面图片(1张图片)
        /// </summary>
        /// <param name="file">上传控件</param>
        /// <returns></returns>
        public (bool 是否合法, string 违法原因, string 文件名) CheckFiles(FileUpload file)
        {
            if (file.PostedFiles.Count == 1)
            {
                //获取传入的上传控件里面的文件名
                string FileName = file.FileName;
                //截取文件后缀
                string FileFex = FileName.Substring(FileName.LastIndexOf(".") + 1).ToLower();
                if (FileFex != "jpg" && FileFex != "png" && FileFex != "jpeg" && FileFex != "gif")
                {
                    return (false, "上传的文件不是图片类型", FileName);
                }
                else
                {
                    return (true, "", FileName);
                }
            }
            else
            {
                return (false, "选择上传的文件超过一个", "");
            }
        }


    
        /// <summary>
        /// 根据省份对应刷新省份
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ctiy1_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (ctiy1.SelectedValue)
            {
                case "广东省":
                    ctiy2.Items.Clear();
                    ctiy2.Items.Add("珠海市");
                    ctiy2.Items.Add("佛山市");
                    ctiy2.Items.Add("汕头市");
                    ctiy2.Items.Add("广州市");
                    ctiy2.Items.Add("深圳市");
                    break;
                case "广西省":
                    ctiy2.Items.Clear();
                    ctiy2.Items.Add("南宁市");
                    ctiy2.Items.Add("柳州市");
                    ctiy2.Items.Add("桂林市");
                    ctiy2.Items.Add("梧州市");
                    ctiy2.Items.Add("北海市");
                    break;
                case "湖北省":
                    ctiy2.Items.Clear();
                    ctiy2.Items.Add("武汉市");
                    ctiy2.Items.Add("黄石市");
                    ctiy2.Items.Add("襄阳市");
                    ctiy2.Items.Add("荆州市");
                    ctiy2.Items.Add("宜昌市");
                    break;
                default:
                    break;
            }
        }
    }
}


